﻿using System;
using fastCSharp;

namespace diantou.web.server
{
    /// <summary>
    /// 静态文件服务
    /// </summary>
    sealed class staticFileServer : fastCSharp.net.tcp.http.domainServer.staticFileServer
    {
        /// <summary>
        /// 访问控制权限数据
        /// </summary>
        private static pointer.size originData;
        unsafe static staticFileServer()
        {
            string domain = diantou.dataModel.path.domain.Default.MainDomain;
            fixed (char* domainFixed = domain)
            {
                originData.Data = unmanaged.Get(originData.Size = domain.Length + 1, false).Data;
                *originData.Byte = (byte)'.';
                fastCSharp.unsafer.String.WriteBytes(domainFixed, domain.Length, originData.Byte + 1);
            }
        }
        /// <summary>
        /// 是否支持访问控制权限
        /// </summary>
        /// <param name="isSsl">是否SSL链接</param>
        /// <returns></returns>
        protected unsafe override bool isOrigin(subArray<byte> origin, bool isSsl)
        {
            if (origin.Count >= originData.Size + 7)
            {
                fixed (byte* originFixed = origin.UnsafeArray)
                {
                    byte* start = originFixed + origin.StartIndex, end = start + origin.Count;
                    if ((*(uint*)start | 0x20202020U) == (uint)('h' + ('t' << 8) + ('t' << 16) + ('p' << 24)))
                    {
                        switch (*(start + sizeof(int)))
                        {
                            case (byte)'s':
                            case (byte)'S':
                                if ((*(uint*)(start + sizeof(int)) | 0x20U) == (uint)('s' + (':' << 8) + ('/' << 16) + ('/' << 24)))
                                {
                                    start += 8;
                                    break;
                                }
                                return false;
                            case (byte)':':
                                if ((*(uint*)(start + 3) | 0x20U) == (uint)('p' + (':' << 8) + ('/' << 16) + ('/' << 24)))
                                {
                                    start += 7;
                                    break;
                                }
                                return false;
                            default:
                                return false;
                        }
                        byte* split = fastCSharp.unsafer.memory.Find(start, end, (byte)'/');
                        if (split != null) end = split;
                        if ((end -= originData.Size) > start)
                        {
                            return fastCSharp.unsafer.memory.SimpleEqual(originData.Byte, end, originData.Size);
                        }
                    }
                }
            }
            return false; 
        }
        /// <summary>
        /// 网站生成配置
        /// </summary>
        /// <returns>网站生成配置</returns>
        protected override fastCSharp.code.webConfig getWebConfig()
        {
            return webServer.WebConfig;
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="extensionName">文件名称信息</param>
        /// <returns>文件内容</returns>
        protected override subArray<byte> readCacheFile(subString extensionName)
        {
            subArray<byte> data = webConfig.ReadCacheFile(ref extensionName, int.MaxValue, WebConfig.IsFileCacheHeader);
            return data.Count == 0 ? base.readCacheFile(extensionName) : data;
        }
    }
}
